package _0_singleton

import (
	"fmt"
	"sync"
)

//Singleton 接口类型避免被New
type Singleton interface {
	foo()
}

//singleton 实际单例类型
type singleton struct {
}

// 实现单例的接口
func (s *singleton) foo() {
	fmt.Println("type singleton struct implement foo()")
}

var (
	// 单例实例
	singletonInstance *singleton
	//保障只会执行一次
	once sync.Once
)

// GetSingletonInstance 获取单例
func GetSingletonInstance() Singleton {
	once.Do(func() {
		singletonInstance = &singleton{}
	})
	return singletonInstance
}
